package com.unicom.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.reg.RegisterStepTwoPage;
import com.unicom.projt.util.AlertDialogUtils;
import com.unicom.projt.util.CheckUtils;
import com.unicom.projt.util.PageJumpUtils;
import com.unicom.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-2-20
 * Time: 下午9:56
 */
public class PaymentInputPhoneNumberPage extends Activity implements View.OnClickListener  {
    private EditText mPhoneNumber;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 19;
    private  PaymentInputPhoneNumberController controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment_input_number);
        controller = new PaymentInputPhoneNumberController(this);

        ((TextView)this.findViewById(R.id.payment_input_number_number_txt)).setText(UserHolder.phoneNumber);
        mPhoneNumber=(EditText)this.findViewById(R.id.payment_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);

        this.findViewById(R.id.payment_input_number_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_input_number_back_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_next_btn).setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.payment_input_number_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
            case R.id.payment_input_number_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.payment_next_btn:
                goToNextPaymentPage();
                break;
        }
    }

    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void goToNextPaymentPage() {
        if (mPhoneNumber.getText().toString().trim().equals("")) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入手机号");
            return;
        } else {
            if (!CheckUtils.isMobileNo(mPhoneNumber.getText().toString().trim())) {
                AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的手机号");
                return;
            }
            this.showDialog(DIALOG_PROGRESS);
            controller.checkThePaymentPhoneNumber(mPhoneNumber.getText().toString().trim());
//            Intent intent = new Intent(this, PaymentPage.class);
//            intent.putExtra("phoneNumber",mPhoneNumber.getText().toString().trim());
//            this.startActivity(intent);
//            this.finish();
        }
    }



    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
